package com.zhang.a_sync_confirm;

import com.rabbitmq.client.*;
import org.junit.Test;

import java.io.IOException;
import java.util.Collections;
import java.util.SortedSet;
import java.util.TreeSet;


public class A_producer {

    @Test
    public void test1( ) throws Exception {
        //1.创建Connection
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.200.114");
        factory.setPort(5672);
        factory.setVirtualHost("/");
        factory.setUsername("guest");
        factory.setPassword("guest");
        Connection connection = factory.newConnection();

        //2.创建channel
        Channel channel = connection.createChannel();

        //3.创建交换机(这里使用的是默认交换机 AMQP_Default)
        channel.exchangeDeclare("e1", BuiltinExchangeType.DIRECT, false, false, false, null);

        //(1)开启确认模式
        channel.confirmSelect();

        //4.创建队列
        channel.queueDeclare("q1", false, false, false, null);
        channel.queueDeclare("q2", false, false, false, null);

        //5.交换机与队列绑定(指定路由key)
        channel.queueBind("q1", "e1", "aaa");
        channel.queueBind("q2", "e1", "bbb");


        //6.发送消息
        channel.basicPublish("e1", "aaa", null, "hello world".getBytes());


        //(2)普通确认 (确认了就返回true)
//        if (channel.waitForConfirms()) {
//            System.out.println("确认消费者收到了单条消息");
//        } else {
//            System.out.println("消费者未能收到单条消息");
//        }

        //(2)批量确认模式 (只要有一条未确认就抛异常)
        channel.waitForConfirmsOrDie();

    }
}

















